package top.dancun.eascourse.controller;

import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import top.dancun.common.utils.DataUtils;
import top.dancun.eascourse.entity.CourseEntity;
import top.dancun.eascourse.service.CourseService;
import top.dancun.common.utils.PageUtils;
import top.dancun.common.utils.R;



/**
 * 
 *
 * @author dancun
 * @email message@dancun.top
 * @date 2022-10-15 11:12:43
 */
@RestController
@RequestMapping("/course")
public class CourseController {
    @Autowired
    private CourseService courseService;

    /**
     * 列表分页
     * @param  params
     * @return R:{code:20000,page:[]}
     */
    @RequestMapping("/list")
    @RequiresPermissions("eascourse:course:list")
    public R list(@RequestBody Map<String, Object> params){
        PageUtils page = courseService.queryPage(params);

        return R.ok().put("page", page);
    }

    /**
     * 查询列表不分页
     * @param params
     * @return R:{code:20000,data:[]}
     */
    @RequestMapping("/listnopage")
    @RequiresPermissions("eascourse:course:list")
    public R listNoPage(@RequestBody Map<String, Object> params){
        //反射字段
        Field[] fields = CourseEntity.class.getFields();
        String[] fieldStr = new String[fields.length];
        for(int i=0;i<fields.length;i++){
            fieldStr[i] = fields[i].getName();
        }
        //接收数据预处理
        Map<String,String> dataParams = new HashMap<>();

        DataUtils.mapAddNoEmpty(dataParams,params,fieldStr);
        List<CourseEntity> list =  courseService.query(dataParams);

        return R.ok().put("data", list);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("eascourse:course:info")
    public R info(@PathVariable("id") Long id){
		CourseEntity course = courseService.getById(id);

        return R.ok().put("course", course);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("eascourse:course:save")
    public R save(@RequestBody CourseEntity course){
		courseService.save(course);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("eascourse:course:update")
    public R update(@RequestBody CourseEntity course){
		courseService.updateById(course);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("eascourse:course:delete")
    public R delete(@RequestBody Long[] ids){
		courseService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

    /**
     * 根据id数组查询
     */
    @RequestMapping("/listbyids")
    @RequiresPermissions("eascourse:course:delete")
    public R listByIds(@RequestBody List<Integer> ids){
        List<CourseEntity> courseList = courseService.listByIds(ids);

        return R.ok().put("data",courseList);
    }

}
